package util;

import GUI.JanelaLogon;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.MaskFormatter;

public class ResourceManager extends JWindow {

	private static final long serialVersionUID = 1L;	
	private final String[] extFotos = {"GIF", "JPG", "JPEG", "PNG"};	
	
	public static final Color corPreta1 = new Color(0, 0, 0);
	public static final Color corAzul1 = new Color(140, 174, 234);
	public static final Color corAzul2 = new Color(183, 209, 255);
	public static final Color corAzul3 = new Color(202, 220, 244);
	public static final Color corVermelha1 = new Color(255, 0, 0);
	public static final Color corBranca1 = new Color(255, 255, 255);
	public static final Font calibri16 = new Font("Calibri", 0, 16);
	public static final Font tahoma11 = new Font("Tahoma", 0, 11);
	public static final Font tahoma18 = new Font("Tahoma", 0, 18);
	public static final DecimalFormat formatoGrana = new DecimalFormat("0.00");
	
	public static boolean LookAndFeelOK = true;

	public static final String[] imgPaths = { 
		"Agenda.png",
		"Agenda2.png",
		"AgendaM.png",
		"Alter.png",
		"AppIcon.png",
		"Calendar.png",
		"Cash.png",
		"Cash2.png",
		"CashM.png",
		"Doctor.png",
		"Doctor2.png",
		"DoctorM.png",
		"Exit.png",
		"Help.png",
		"Info.png",
		"Key.png",
		"LayoutMaior.png",
		"LayoutMenor1.png",
		"LayoutMenor2.png",
		"Logo.png",
		"Ok.png",
		"Paciente.png",
		"Paciente2.png",
		"PacienteM.png",
		"Person.png",
		"Prontuario.png",
		"Prontuario2.png",
		"ProntuarioM.png",
		"Ret.png",
		"SemFoto.png",
		"setaLogin.png",
		"setaLogin2.png",
		"Welcome.png",
	};
	
	private String imgBase = "imagens/";
	private HashMap<String, ImageIcon> map = null;
	private JProgressBar bar = null;
	private BufferedImage back = null;
	
	private JFileChooser browser = null;
	private MaskFormatter formatoCPF = null;
	private MaskFormatter formatoData = null;
	private MaskFormatter formatoAno = null;
	private MaskFormatter formatoFone = null;
	private MaskFormatter formatoHorario = null;
	
	public ResourceManager() throws IOException {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			ResourceManager.LookAndFeelOK = false;
		}
		
		this.criarBrowser();
		this.criarFormatoCPF();
		this.criarFormatoData();
		this.criarFormatoAno();
		this.criarFormatoFone();
		this.criarFormatoHorario();

		map = new HashMap<String, ImageIcon>();
		back = ImageIO.read(ClassLoader.getSystemResource(imgBase + "splash.png"));

		this.setLayout(null);
		this.setSize(400, 300);
		this.setLocationRelativeTo(null);

		bar = new JProgressBar(0, imgPaths.length);
		bar.setSize(390, 20);
		bar.setLocation(5, 275);

		this.add(bar);
		this.setVisible(true);
	}
	
	private void criarFormatoHorario() {
		try { 
			formatoHorario = new MaskFormatter("##:##");
			formatoHorario.setPlaceholderCharacter('0');
		} catch (ParseException e) { 
			e.printStackTrace(); 
		}
	}

	private void criarFormatoFone() {
		try { 
			formatoFone = new MaskFormatter("(##)####-####");
			formatoFone.setPlaceholderCharacter('0');
		} catch (ParseException e) { 
			e.printStackTrace(); 
		}
	}

	private void criarFormatoAno() {
		try { 
			formatoAno = new MaskFormatter("####");
			formatoAno.setPlaceholderCharacter('0');
		} catch (ParseException e) { 
			e.printStackTrace(); 
		}
	}

	private void criarFormatoData() {
		try { 
			formatoData = new MaskFormatter("##-##-####");
			formatoData.setPlaceholderCharacter('0');
		} catch (ParseException e) { 
			e.printStackTrace(); 
		}
	}

	private void criarFormatoCPF() {
		try { 
			formatoCPF = new MaskFormatter("###.###.###-##");
			formatoCPF.setPlaceholderCharacter('0');
		} catch (ParseException e) { 
			e.printStackTrace(); 
		}
	}

	private void criarBrowser() {
		browser = new JFileChooser();
		browser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		browser.setAcceptAllFileFilterUsed(false);
		browser.setFileFilter(new Filtro("Imagens (*.gif; *.jpg; *.jpeg; *.png)", extFotos));
		browser.setDialogTitle("Selecione uma Foto");
		browser.setApproveButtonText("Ok");
		browser.setMultiSelectionEnabled(false);
		browser.setVisible(false);
	}

	public void loadImages() { 
		imageLoader.execute(); 
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(back, 0, 0, this);
		Rectangle r = bar.getBounds();
		bar.paint(g.create(r.x, r.y, r.width, r.height));
	}

	private SwingWorker<Void, ImageIcon> imageLoader = new SwingWorker<Void, ImageIcon>() {
		protected Void doInBackground() throws Exception {
			for (String s : imgPaths) {
				if (!map.containsKey(s)) {
					ImageIcon img = new ImageWrapper(ImageIO.read(ClassLoader.getSystemResource(imgBase + s)));
					map.put(s, img);
					publish(img);
					Thread.sleep(50);
				}
			}
			return null;
		}

		protected void done() {
			setVisible(false);
			new JanelaLogon(getSelf());
		}

		protected void process(List<ImageIcon> chunks) {
			bar.setValue(bar.getValue() + chunks.size());
		}
	};
	
	private ResourceManager getSelf() { 
		return this; 
	}
	
	public ImageIcon getImage(String name) { 
		return map.get(name); 
	}
	
	public JFileChooser getBrowser() {
		return browser;
	}
	
	public MaskFormatter getFormatoHorario() {
		return formatoHorario;
	}
	
	public MaskFormatter getFormatoFone() {
		return formatoFone;
	}
	
	public MaskFormatter getFormatoCPF() {
		return formatoCPF;
	}
	
	public MaskFormatter getFormatoData() {
		return formatoData;
	}
	
	public MaskFormatter getFormatoAno() {
		return formatoAno;
	}

	public static void main(String[] s) {
		try {
			new ResourceManager().loadImages();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getStackTrace(), "Erro ao carregar imagens", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
}
